package com.khotyn.heresy.service;

import java.util.ArrayList;
import java.util.List;

import com.khotyn.heresy.bean.Friend;
import com.khotyn.heresy.bean.UserBrief;
import com.khotyn.heresy.bean.UserDetail;
import com.khotyn.heresy.dao.AlbumDAO;
import com.khotyn.heresy.dao.FriendDAO;
import com.khotyn.heresy.dao.UserDAO;

/**
 * 用户中心服务类
 * 
 * @author 黄挺
 * 
 */
public class UserService {
	private UserDAO userDAO;
	private AlbumDAO albumDAO;
	private FriendDAO friendDAO;

	/**
	 * 用户中心服务函数
	 * 
	 * @param userID 用户ID
	 * @param requestUrl 请求URL
	 * @return 用户详细Bean
	 * @throws Exception 用户不存在或者用户未激活
	 */
	public UserDetail doService(Integer userID) throws Exception {

		UserDetail userDetail = doDBOperate(userID);

		return userDetail;
	}

	/**
	 * 用户中心数据操作类
	 * 
	 * @param userID 用户ID
	 * @param browseLog 日志Bean
	 * @return 用户详细Bean
	 * @throws Exception
	 */
	public UserDetail doDBOperate(Integer userID) throws Exception {
		UserDetail userDetail = userDAO.selectUserByUserId(userID);
		// 不存在的用户
		if (userDetail == null)
			throw new Exception("不存在的用户");

		// 未激活的用户
		if (userDetail.getFlagActive().toLowerCase().equals("notactived"))
			throw new Exception("用户尚未激活，请先激活");

		userDetail.setAlbums(albumDAO.selectPagedAlbumByUserID(userID, 0, 10));

		// 获取用户好友
		List<Friend> friends = friendDAO.selectPagedFriendByUserID(userID, 0, 6);
		List<UserBrief> users = new ArrayList<UserBrief>();

		for (Friend friend : friends)
			users.add(friend.getFriend());

		userDetail.setFriends(users);

		return userDetail;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setAlbumDAO(AlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}

	public void setFriendDAO(FriendDAO friendDAO) {
		this.friendDAO = friendDAO;
	}
}
